<?php

require_once 'Controller.php';

class GroupController extends Controller {
    
    public function groupsAction(){
        $this->_autoRender = false;
        $this->_view->layout->title = 'Groups Page';
        try {
            $this->_view->groups = $this->_model->all($this->_request->getQuery());
        } catch (Exception $ex) {
            return $this->_response->fileNotFound();
        }
        $this->_view->scol = $this->_request->getQuery('sc', 'id');
        $this->_view->sord = $this->_request->getQuery('so', 'asc');
        $this->_view->fcol = $this->_request->getQuery('fc', 'id');
        $this->_view->fval = $this->_request->getQuery('fv', '');
        $this->_response->render('group/groups');
    }
    
    public function addAction(){
        $this->_view->layout->title = 'Add New Group';
        $new = null;
        if ( $this->_request->isPost() ){
            $group_name = $this->_request->getPost('name', null);
            $new = array('name' => $group_name,);
            $this->_validator->notBlank('Group Name', $group_name);
            if ( $this->_validator->isValid() ){
                try{
                    $this->_model->add($group_name);
                    $this->_response->redirect('/index.php/group/groups');
                } catch (Exception $ex){
                    $this->_validator->addError('SQL ERROR', $ex->getMessage());
                }
            }
        }
        $this->_view->new = $new;
        $this->_view->errors = $this->_validator->getErrors();
    }
    
    public function editAction(){
        $this->_autoRender = false;
        $this->_view->layout->title = 'Edit Group';
        $id = $this->_request->getQuery('id', null);
        $group = $this->_model->findById($id);
        if ( ! $group ){
            return $this->_response->fileNotFound();
        }
        if ( $this->_request->isPost() ){
            $name = $this->_request->getPost('name', null);
            $id = $this->_request->getPost('id', null);
            $this->_validator->notBlank('Group Name', $name);
            if ( $this->_validator->isValid() ){
                try {
                    $this->_model->edit($id, $name);
                    $this->_response->redirect('/index.php/group/groups');
                } catch(Exception $ex){
                    $this->_validator->addError('SQL ERROR', $ex->getMessage());
                }
            }
        }
        $this->_view->group = $group;
        $this->_view->errors = $this->_validator->getErrors();
        $this->_response->render('group/edit');
    }
    
    public function delAction(){
        $this->_autoRender = false;
        if ( ! $this->_request->isPost() ){
            return $this->_response->fileNotFound();
        }
        $id = $this->_request->getPost('id', null);
        try {
            $this->_model->del($id);
        } catch (Exception $ex) {
            
        }
        $this->_response->redirect('/index.php/group/groups');
    }
    
}
